package com.celebracion;

import com.celebracion.util.DBUtil;
import com.celebracion.util.FileUtil;
import com.celebracion.util.SystemUiHider;

import android.annotation.TargetApi;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.widget.MediaController;
import android.widget.Toast;
import android.widget.VideoView;

/**
 * Acabado
 */
public class VideoPreviewActivity extends Activity {
	
	
	String fileTemp;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		
		setContentView(R.layout.activity_video_preview);

		Bundle extra = this.getIntent().getExtras();
		fileTemp=extra.getString("video");
		final String video="file://"+fileTemp;
		//final String video="android.resource://"+fileTemp;
		 final VideoView videoView = (VideoView) findViewById(R.id.videoView_video);
		 //Uri path = Uri.parse("android.resource://"+video);
		Log.d("video", video);
		videoView.setVideoPath(video);
		
		  MediaController mc = new MediaController(this); 
		  mc.setVisibility(View.VISIBLE);
		videoView.setMediaController(mc);
		videoView.setOnCompletionListener(new OnCompletionListener(){

			@Override
			public void onCompletion(MediaPlayer mp) {
				videoView.setVideoPath(video);
				videoView.start();
				
				
			}});
		
		videoView.start();
	
		
	}
	
	public void atras(View view){
		AlertDialog.Builder builder = new AlertDialog.Builder(this);

		// 2. Chain together various setter methods to set the dialog characteristics
		builder.setMessage(R.string.askDeleteVideo)
		       .setTitle(R.string.askTitleDeleteVideo);
		// Add the buttons
		builder.setPositiveButton(R.string.Si, new DialogInterface.OnClickListener() {
		           public void onClick(DialogInterface dialog, int id) {
		        	   FileUtil.deleteTempFile(fileTemp);
		        	   Intent intent=new Intent(VideoPreviewActivity.this,NombresActivity.class);
		        	   //Vacia la pila de la tarea hasta esta 
		        	   intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
		       		   startActivity(intent);
		           }
		       });
		builder.setNegativeButton(R.string.No, new DialogInterface.OnClickListener() {
		           public void onClick(DialogInterface dialog, int id) {
		        	  
		           }
		       });
		// 3. Get the AlertDialog from create()
		AlertDialog dialog = builder.create();
		dialog.show();
		
	}
	
	public void guardar(View view){
		if(DBUtil.getInstance(getBaseContext()).getEventoActivo().getId()==-1){
			Toast.makeText(this, R.string.toast_modo_pruebas, Toast.LENGTH_LONG).show();
		}else{
			AlertDialog.Builder builder = new AlertDialog.Builder(this);
	
			// 2. Chain together various setter methods to set the dialog characteristics
			builder.setMessage(R.string.askSaveVideo)
			       .setTitle(R.string.askTitleSaveVideo);
			// Add the buttons
			builder.setPositiveButton(R.string.Si, new DialogInterface.OnClickListener() {
			           public void onClick(DialogInterface dialog, int id) {
			        	   FileUtil.saveTempToFile(fileTemp, DBUtil.getInstance(VideoPreviewActivity.this.getBaseContext()).getEventoActivo().getId(),VideoPreviewActivity.this.getBaseContext());
			        	   Intent intent=new Intent(VideoPreviewActivity.this,NombresActivity.class);
			        	   intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
			       		   startActivityForResult(intent, 333);
			       		
			       		   
			           }
			       });
			builder.setNegativeButton(R.string.No, new DialogInterface.OnClickListener() {
			           public void onClick(DialogInterface dialog, int id) {
			        	  
			           }
			       });
			// 3. Get the AlertDialog from create()
			AlertDialog dialog = builder.create();
			dialog.show();
		}
		

	}
	
	
}
